package ecoll.client.widgets;

import com.google.gwt.appengine.channel.client.Socket;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import ecoll.client.Client;

public class QuizRoomPanel extends Grid {

	public QuizRoomPanel() {
		super(1, 2);
		clear();
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public void setServerArea(QuizServerPanel serverArea) {
		this.serverArea = serverArea;
		quizArea.setWidget(0, 0, serverArea);
		serverArea.setWidth("100%");
	}

	public void setChatArea(VerticalPanel chatArea) {
		this.chatArea = chatArea;
		ScrollPanel chatScrollArea = new ScrollPanel(chatArea);
		chatScrollArea.setStyleName("chatArea");
		chatArea.setWidth("100%");
		chatArea.setHeight("100%");
		quizArea.setWidget(1, 0, chatScrollArea);

		// chatArea.setStyleName("chatArea");
		chatScrollArea.setWidth("100%");
		chatScrollArea.setHeight("280px");
	}

	public void setUserArea(QuizUserPanel userArea) {
		this.userArea = userArea;
		quizArea.setWidget(2, 0, userArea);
		userArea.setWidth("100%");
	}

	public void setRankingArea(QuizRankingPanel rankingArea) {
		this.rankingArea = rankingArea;
		VerticalPanel fullRankingArea = new VerticalPanel();
		fullRankingArea.add(rankingArea);
		Client.fillArea(fullRankingArea);
		setWidget(0, 1, fullRankingArea);

		rankingArea.setStyleName("rankingArea");
		fullRankingArea.setHeight("100%");
		fullRankingArea.setWidth("100%");
	}

	public void addServerTopMessage(String message) {
		if (serverArea != null)
			serverArea.setFirtsRow(message);
	}

	public void addServerBottomMessage(String message) {
		if (serverArea != null)
			serverArea.setSecondRow(message);
	}

	public void addChatMessage(String message) {
		if (chatArea == null)
			return;
		Label chatEntry = new Label(message);
		chatEntry.setStyleName("chatLabel");
		chatArea.add(chatEntry);
		((ScrollPanel) chatArea.getParent()).scrollToBottom();
	}

	public void addUserScore(String login) {
		if (rankingArea != null)
			rankingArea.addUserScore(login);
	}

	public void addUserRankData(String login) {
		if (rankingArea != null)
			rankingArea.addUserRankData(login, 0);
	}

	public void removeUserRankData(String login) {
		if (rankingArea != null)
			rankingArea.removeUserRankData(login);
	}

	@Override
	public void clear() {
		super.clear();
		if (socket != null)
			socket.close();
		socket = null;

		serverArea = null;
		chatArea = null;
		userArea = null;
		rankingArea = null;

		CellFormatter formatter = getCellFormatter();
		formatter.setWidth(0, 0, "75%");
		formatter.setHeight(0, 0, "100%");
		formatter.setWidth(0, 1, "25%");
		formatter.setHeight(0, 1, "100%");

		setQuizArea();
	}

	@Override
	protected void onUnload() {
		super.onUnload();
		clear();
	}

	private void setQuizArea() {
		quizArea = new Grid(3, 1);
		setWidget(0, 0, quizArea);
		quizArea.setWidth("100%");
		quizArea.setHeight("100%");

		CellFormatter formatter = quizArea.getCellFormatter();
		formatter.setWidth(0, 0, "100%");
		formatter.setHeight(0, 0, "15%");
		formatter.setWidth(1, 0, "100%");
		formatter.setHeight(1, 0, "70%");
		formatter.setWidth(2, 0, "100%");
		formatter.setHeight(2, 0, "15%");
	}

	private Socket socket;
	private VerticalPanel chatArea;
	private Grid quizArea;
	private QuizRankingPanel rankingArea;
	private QuizUserPanel userArea;
	private QuizServerPanel serverArea;
}
